package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.dao.CustomerMapper;
import com.qfedu.openplateform.manager.entity.Customer;
import com.qfedu.openplateform.manager.entity.TableData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/sys/customer")
public class CustomerController {

    @Autowired
    private CustomerMapper customerMapper;

    @RequestMapping("/table") //查询所有客户
    public TableData<Customer> SelectAllCustomer(int page,int limit,Integer state, String name) {

        TableData<Customer> TableData = new TableData();

        if (name == null) {

            List<Customer> customers = customerMapper.SelectAllCustomer();

            PageHelper.startPage(page, limit);

            PageInfo pageInfo = new PageInfo(customers);

            TableData.setCount(pageInfo.getTotal());

            TableData.setData(customers);
        } else if (name!= null) {

            List<Customer> customers1 = customerMapper.SelectCustomerByName(state,name);

            PageHelper.startPage(page, limit);

            PageInfo pageInfo1 = new PageInfo(customers1);

            TableData.setCount(pageInfo1.getTotal());

            TableData.setData(customers1);

            return TableData;

        }

        return TableData;
    }
    @RequestMapping("/update")
    public TableData updateCustomer(Customer customer) {

        int i = customerMapper.updateCustomer(customer);

        TableData<Object> TableData = new TableData<>();

        if (i > 0) {

            TableData.setCode(1);

            TableData.setMsg("修改成功");
        }else {

            TableData.setMsg("修改失败");
        }

        return TableData;
    }

    @RequestMapping("/del")
    public TableData deleteCustomer(Integer ids){

        TableData<Object> TableData = new TableData<>();

        int i = customerMapper.deleteCustomer(ids);

        if (i > 0) {

            TableData.setCode(1);

            TableData.setMsg("删除成功");
        }else {

            TableData.setMsg("删除失败");
        }
        return TableData;

    }

    @RequestMapping("/add")
    public TableData addCustomer(Customer customer){

        TableData<Customer> TableData = new TableData<>();

        int i = customerMapper.addCustomer(customer);

        if (i > 0) {

            TableData.setCode(1);

            TableData.setMsg("添加成功");
        }else {

            TableData.setMsg("添加失败");
        }
        return TableData;
    }

    @RequestMapping("/tree")
    public List<Customer> updateAppInfo(){

/*
        TableData<Customer> TableData = new TableData();
*/

        List<Customer> customers = customerMapper.SelectAllCustomer();

       /* int i = appInfoMapper.updateAppInfo(appInfo);

        if (i > 0) {

            TableData.setData(customers);

            TableData.setCode(1);

            TableData.setMsg("修改成功");
        }else {

            TableData.setMsg("修改失败");
        }*/

/*
        TableData.setData(customers);
*/

        return customers ;

    }
}
